<template>
	<div v-loading="loading">
		<HyForm :hyFormDefault="applyInfoDefault" :hyFormConfig="hyFormConfig" :hyFormFields="hyFormFields" ref="hyFormRef" />
	</div>
</template>

<script setup lang="ts">
import HyForm from '/@/components/HyForm/index.vue';
import { defaultValue } from '../../utils/default';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useDict } from '/@/hooks/dict';
const { applyInfoDefault } = defaultValue();
// 定义字典
const { culture_type, fund_resource, app_construction_level, app_construction_mode, yes_no_type } = useDict(
	'culture_type',
	'fund_resource',
	'app_construction_level',
	'app_construction_mode',
	'yes_no_type'
);
const loading = ref(false);
const hyFormRef = ref();
const props = defineProps({
	applyInfoData: {
		type: Object,
		default: () => defaultValue().applyInfoDefault,
	},
	routerParams: {
		type: Object,
		default: () => {
			return {};
		},
	},
});

watch(
	[() => props.applyInfoData],
	(nVal, oVal) => {
		nextTick(() => {
			const basicInfoData = JSON.parse(JSON.stringify(nVal[0]));
			loading.value = true;
			hyFormRef.value.hyForm = basicInfoData;
			bpmCapitalSourceChange(basicInfoData.fundResource);
			workTeamChange(basicInfoData.appConstructionType);
			loading.value = false;
		});
	},
	{ deep: true }
);
//动态表单配置
const hyFormConfig = computed(() => {
	return {
		labelWidth: '150px',
		span: 8,
		offset: 0,
		type: 'submit',
	};
});
//控制应用建设类型显示
const isWork = ref(false);
const isTeam = ref(false);
const isShow = ref(false);
const workTeamChange = (option: any, type=false) => {
	if (type) {
		isShow.value = true;
	} else {
		isShow.value = false;
	}
	if (option == 1) {
		isWork.value = true;
		isTeam.value = false;
	} else if (option == 2) {
		isWork.value = true;
		isTeam.value = true;
	}
};
watch(
	() => isShow.value,
	(newValue: any) => {
		nextTick(() => {
			hyFormRef.value.hyForm.projectType = '';
		});
	},
	{ immediate: false }
);
//控制资金来源显示
const yearlyBudgeta = ref(false);
const yearlyBudgetb = ref(false);
const yearlyBudgetc = ref(false);
const yearlyBudgetd = ref(false);
const yearlyBudgete = ref(false);
const yearlyBudgetf = ref(false);

const bpmCapitalSourceChange = (options: any) => {
	options.includes('1') ? (yearlyBudgetb.value = true) : ((yearlyBudgetb.value = false), (hyFormRef.value.hyForm.centerAmount = ''));
	options.includes('2') ? (yearlyBudgetc.value = true) : ((yearlyBudgetc.value = false), (hyFormRef.value.hyForm.cityAmount = ''));
	options.includes('3') ? (yearlyBudgetd.value = true) : ((yearlyBudgetd.value = false), (hyFormRef.value.hyForm.districtAmount = ''));
	options.includes('4') ? (yearlyBudgete.value = true) : ((yearlyBudgete.value = false), (hyFormRef.value.hyForm.deptAmount = ''));
	options.includes('5') ? (yearlyBudgetf.value = true) : ((yearlyBudgetf.value = false), (hyFormRef.value.hyForm.bankAmount = ''));
	options.includes('6')
		? (yearlyBudgeta.value = true)
		: ((yearlyBudgeta.value = false), (hyFormRef.value.hyForm.otherFund = ''), (hyFormRef.value.hyForm.otherAmount = ''));
};
//配置基本信息表单
const hyFormFields = computed(() => {
	return {
		appName: {
			label: '应用名称',
			type: 'input',
			required: true,
			maxlength: 50,
		},
		principalName: {
			label: '应用负责人',
			type: 'input',
			required: true,
			maxlength: 16,
		},
		principalPhone: {
			label: '负责人手机号码',
			type: 'input',
			required: true,
			maxlength: 11,
			rules: [
				{
					pattern: /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/,
					message: '请输入正确的手机号码格式',
					trigger: 'blur',
				},
			],
		},
		appConstructionType: {
			label: '应用建设类型',
			type: 'radio',
			required: true,
			options: [
				{ label: '开发应用', value: '1' },
				{ label: '运维应用', value: '2' },
			],
			change: (option: any) => workTeamChange(option, true),
		},
		projectType: {
			label: '建设方式',
			type: 'radio',
			required: true,
			options: !isTeam.value
				? [
						{ label: '新建', value: '1' },
						{ label: '续建', value: '2' },
				  ]
				: [
						{ label: '首次运维', value: '3' },
						{ label: '连续运维', value: '4' },
				  ],
			remove: !isWork.value,
		},

		budgetYear: {
			label: '应用预算年度',
			type: 'date',
			picker: 'year',
			required: true,
			showFormat: 'YYYY',
			span: 7,
		},

		developBeginDate: {
			type: 'date',
			picker: 'date',
			required: true,
			label: '应用建设开始时间',
			showFormat: 'YYYY-MM-DD',
			change: (e) => {
				const { developEndDate } = hyFormRef.value.hyForm;
				if (developEndDate) {
					const _start = new Date(e).getTime();
					const _end = new Date(developEndDate).getTime();
					if (_start - _end > 0) {
						hyFormRef.value.hyForm.developBeginDate = '';
						return ElMessage.error('起始时间不能在结束时间后');
					}
				}
			},
		},

		developEndDate: {
			type: 'date',
			picker: 'date',
			required: true,
			label: '应用建设结束时间',
			showFormat: 'YYYY-MM-DD',
			change: (e) => {
				const { developBeginDate } = hyFormRef.value.hyForm;
				if (developBeginDate) {
					const _end = new Date(e).getTime();
					const _start = new Date(developBeginDate).getTime();
					if (_start - _end > 0) {
						hyFormRef.value.hyForm.developEndDate = '';
						return ElMessage.error('终止时间不能在开始时间前');
					}
				}
			},
		},

		domain: {
			label: '应用领域',
			type: 'select',
			options: culture_type.value,
			placeholder: '请选择',
			// change: (option: any) => change(option),
		},

		totalBudget: {
			label: '应用总投资（万元）',
			required: true,
			type: 'inputFunny',
		},
		yearBudget: {
			label: '应用年度预算(万元)',
			required: true,
			type: 'inputFunny',
		},
		fundResource: {
			label: '资金来源',
			type: 'select',
			options: fund_resource.value,
			required: true,
			multiple: true,
			change: (option: any) => bpmCapitalSourceChange(option),
			// change: (option: any) => change(option),
		},
		otherFund: {
			label: '其它来源名称',
			required: true,
			type: 'input',
			placeholder: '请输入名称',
			remove: !yearlyBudgeta.value,
			maxlength: 50,
		},
		otherAmount: {
			label: '其它资金',
			placeholder: '请输入金额',
			required: true,
			type: 'inputFunny',
			remove: !yearlyBudgeta.value,
		},
		centerAmount: {
			label: '中央财政资金',
			required: true,
			type: 'inputFunny',
			placeholder: '请输入金额',
			remove: !yearlyBudgetb.value,
		},
		cityAmount: {
			label: '市级财政资金',
			required: true,
			type: 'inputFunny',
			placeholder: '请输入金额',
			remove: !yearlyBudgetc.value,
		},
		districtAmount: {
			label: '区县财政资金',
			placeholder: '请输入金额',
			required: true,
			type: 'inputFunny',
			remove: !yearlyBudgetd.value,
		},
		deptAmount: {
			label: '部门自筹资金',
			placeholder: '请输入金额',
			required: true,
			type: 'inputFunny',
			remove: !yearlyBudgete.value,
		},
		bankAmount: {
			label: '银行贷款等投资',
			placeholder: '请输入金额',
			required: true,
			type: 'inputFunny',
			remove: !yearlyBudgetf.value,
		},

		hierarchy: {
			label: '建设层级',
			type: 'select',
			options: app_construction_level.value,
			required: true,
			multiple: true,
			// change: (option: any) => change(option),
		},
		mode: {
			label: '建设模式',
			type: 'select',
			options: app_construction_mode.value,
			required: true,
			multiple: true,
			// change: (option: any) => change(option),
		},
		localization: {
			label: '是否国产化',
			type: 'select',
			options: [
				{ label: '否', value: '0' },
				{ label: '是', value: '1' },
			],
			required: true,
			// change: (option: any) => change(option),
		},
	};
});
const getData = () => {
	const formData = hyFormRef.value.hyForm;
	return {
		formData,
	};
};
const applyInfocheck = async () => {
	return await hyFormRef.value.check(false);
};
defineExpose({ getData, applyInfocheck });
</script>

<style lang="scss" scoped></style>
